﻿using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.EventSystems;

public class GearEditor : UnitEditor
{
    public Gear Gear
    {
        get { return GB.Gear; }
        set { GB.Gear = value; }
    }

    public GearBehaviour GB
    {
        get { return UB as GearBehaviour; }
        set { UB = value; }
    }

    protected override void Update()
    {
        var rt = GetComponent<RectTransform>();
        var s = EMgr.View2WorldScale;
        rt.sizeDelta = new Vector2(Gear.Radius * 2.2f / s + 10, Gear.Radius * 2.2f / s + 10);
        transform.localRotation = GB.transform.localRotation;
        base.Update();
    }

    protected override void OnClick(Vector2 pt)
    {
        base.OnClick(pt);
        ShowOpMenu();
    }

    void AdjustAngle()
    {
        var angle = Gear.Angle.RangeInPi2();
        if (angle < Mathf.PI / 4)
            angle = 0;
        else if (angle < Mathf.PI * 3 / 4)
            angle = Mathf.PI / 2;
        else if (angle < Mathf.PI * 5 / 4)
            angle = Mathf.PI;
        else if (angle < Mathf.PI * 7 / 4)
            angle = Mathf.PI * 3 / 2;
        else
            angle = 0;

        Gear.Angle = angle;
    }

    public override void OnDrag(PointerEventData eventData)
    {
        base.OnDrag(eventData);
        AdjustAngle();
    }

    public override void OnEndDrag(PointerEventData eventData)
    {
        AdjustAngle();
        base.OnEndDrag(eventData);
    }

    void ShowOpMenu()
    {
        var opg1 = new List<string>();
        var opg2 = new List<string>();

        opg1.Add("X");

        if (Gear.Radius < 3)
            opg1.Add("+");

        if (Gear.Radius > 1)
            opg1.Add("-");

        if (Gear.Convex)
        {
            opg1.Add("普通");
            opg1.Add("旋转");
        }
        else
            opg1.Add("凸轮");

        if (Gear.ClockwiseThooth && Gear.CounterClockwiseThooth)
        {
            opg2.Add("光滑");
            opg2.Add("顺齿");
            opg2.Add("逆齿");
        }
        else if (Gear.ClockwiseThooth && !Gear.CounterClockwiseThooth)
        {
            opg2.Add("光滑");
            opg2.Add("逆齿");
            opg2.Add("双向");
        }
        else if (!Gear.ClockwiseThooth && Gear.CounterClockwiseThooth)
        {
            opg2.Add("光滑");
            opg2.Add("顺齿");
            opg2.Add("双向");
        }
        else
        {
            opg2.Add("顺齿");
            opg2.Add("逆齿");
            opg2.Add("双向");
        }

        if (Gear.ToothEndAngle == Mathf.PI)
            opg2.Add("全齿");
        else
            opg2.Add("半齿");

        while (opg1.Count < opg2.Count)
            opg1.Add(null);

        while (opg1.Count > opg2.Count)
            opg2.Add(null);

        EMgr.ShowOpMenuAt(
            EMgr.World2View(Gear.Pos) + Vector3.up * (Gear.Radius * 1.2f / EMgr.View2WorldScale + 20),
            new string[][] { opg1.ToArray(), opg2.ToArray() }, OnOp);
    }

    void OnOp(string op)
    {
        switch(op)
        {
            case "X":
                EMgr.DeleteUnit(this);
                EMgr.CloseOpMenu();
                return;
            case "+":
                Gear.Radius++;
                break;
            case "-":
                Gear.Radius--;
                break;
            case "光滑":
                Gear.ClockwiseThooth = false;
                Gear.CounterClockwiseThooth = false;
                break;
            case "顺齿":
                Gear.ClockwiseThooth = true;
                Gear.CounterClockwiseThooth = false;
                break;
            case "逆齿":
                Gear.ClockwiseThooth = false;
                Gear.CounterClockwiseThooth = true;
                break;
            case "双向":
                Gear.ClockwiseThooth = true;
                Gear.CounterClockwiseThooth = true;
                break;
            case "普通":
                Gear.Convex = false;
                break;
            case "凸轮":
                Gear.Convex = true;
                break;
            case "全齿":
                Gear.ToothStartAngle = 0;
                Gear.ToothEndAngle = Mathf.PI * 2;
                break;
            case "半齿":
                Gear.ToothStartAngle = 0;
                Gear.ToothEndAngle = Mathf.PI;
                break;
            case "旋转":
                Gear.Angle -= Mathf.PI / 2;
                break;
            default:
                return;
        }

        if (Unit.Src != null)
            Unit.Update(0, Unit.Src.GetLinkType(Unit));
        else
            Unit.UpdateAllChildren(0);

        Refresh();
        ShowOpMenu();
    }
}
